var vm = new Vue({
    el: "section",
    data: {
        active: 0,//cant插件：steps步骤条
        active_child: 0,
        select_dept: "105000",
        option_dept: [
            { text: '经济管理学院', value: "105000" },
        ],
        name: "",
        isBtnShow1: false,
        isBtnShow2: false,
        isBtnShow3: false,
        isBtnShow4: false,
        searchResult: [],
        selectResult: "",
        show: false,//vant 上拉菜单
        actions: [
            // {name: '选项'},
            // {name: '选项'},
            // {name: '选项'}
        ],
        phone: "",
        // vant密码输入框
        value: '',//中间四位数
        value2: '',//六位数验证码
        showKeyboard: true,
        showKeyboard2: false,
        send: false,
        msgID: "",



    },
    methods: {
        input(){
            switch (this.active_child){
                case 0: 
                    if ((/^\s*$/.test(this.select_dept)) || (/^\s*$/.test(this.name))) {
                        this.isBtnShow1 = false
                    }else{
                        this.isBtnShow1 = true
                    }
                    break;
            }
            
        },
        selectInfo(huat_id){
            this.selectResult = huat_id
        },
        onSelect(item) {
            // 默认情况下，点击选项时不会自动关闭菜单
            // 可以通过 close-on-click-action 属性开启自动关闭
            console.log("选择操作")
            this.show = false
            this.phone = item.name
            this.active = 1
            this.active_child = 2
        },
        onCancel() {//上拉菜单取消按钮
            this.show = false;
        },
        // 手机号输入框
        onInput(key) {
            this.value = (this.value + key).slice(0, 4);
            if(this.value.length == 4){
                let loading1 = this.$toast.loading({
                    message: "",
                    forbidClick: true,
                    duration: 60000,
                })
                // 发送请求验证手机号
                let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/login`
                let method = 'check_teacher_phone_V98fb415BJ4b1n'
                let data = {
                    huat_id: this.selectResult,
                    phone: `${this.phone.slice(0, 3)}${this.value}${this.phone.slice(-4)}`
                }
                axios.post(`${url}/${method}`, data)
                .then(res=>{
                    loading1.clear()
                    if(res.data.code == 0){
                        if(res.data.data.result){// 比对成功
                            this.active_child = 3
                            // 请求短信发送接口
                            let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/login`
                            let method = 'smsJudgement'
                            let data = {
                                huat_id: this.selectResult,
                                phone: `${this.phone.slice(0, 3)}${this.value}${this.phone.slice(-4)}`,
                            }
                            console.log(data)
                            axios.post(`${url}/${method}`, data)
                            .then(res=>{
                                console.log(res.data)
                                if(res.data.code == 0){
                                    if(res.data.data.code == 0){//已发送短信验证码
                                        this.send = true
                                        this.msgID = res.data.data.msgID
                                    }else{
                                        this.$toast({
                                            message: `${res.data.data.msg}`,
                                            forbidClick: true,
                                            duration: 1800,
                                        })
                                        setTimeout(()=>{
                                            this.active_child = 1
                                            this.phone = ""
                                            this.value = ""
                                        }, 2000)
                                        
                                    }
                                    
                                }else{
                                    this.$toast({
                                        message: `服务出错[${res.data.code}]`,
                                        forbidClick: true
                                    })
                                }
                                
                            })


                        }else{// 比对失败
                            this.$toast({
                                message: `手机号错误`,
                                forbidClick: true
                            })
                            this.value = ""
                        }
                    }else{
                        this.$toast({
                            message: `服务出错[${res.data.code}]`,
                            forbidClick: true
                        })
                    }
                })

            }
        },
        onDelete() {
            this.value = this.value.slice(0, this.value.length - 1);
        },
        // 验证码输入框
        onInput2(key) {
            this.value2 = (this.value2 + key).slice(0, 6);
            if(this.value2.length == 6){
                let loading1 = this.$toast.loading({
                    message: "",
                    forbidClick: true,
                    duration: 60000,
                })
                // 发送请求验证验证码
                let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/login`
                let method = 'smsVerification'
                let data = {
                    deptID: this.select_dept,
                    huat_id: this.selectResult,
                    phone: `${this.phone.slice(0, 3)}${this.value}${this.phone.slice(-4)}`,
                    vcode: this.value2,
                    msgID: this.msgID
                }
                
                
                console.log(data)
                axios.post(`${url}/${method}`, data)
                .then(res=>{
                    loading1.clear()
                    console.log(res.data)
                    if(res.data.smsVerification.code == 0){
                        if(res.data.smsVerification.msg == "验证成功！"){
                            // 注册成功提示
                            this.$dialog.alert({
                                type: 'success',
                                title: '注册成功',
                                message: `您已成功认证并注册教师账户【${this.name.trim()}】，使用工号/手机号+初始密码${res.data.smsVerification.pwd}进行登录。`
                              }).then(() => {
                                // on close
                                window.location.replace("../../login/login.html")
                              });
                        }else{
                            this.$toast({
                                message: `${res.data.smsVerification.msg}`,
                                forbidClick: true
                            })
                        }
                    }else{
                        this.$toast({
                            message: `服务出错[${res.data.smsVerification.code}]`,
                            forbidClick: true
                        })
                    }
                })

            }
        },
        onDelete2() {
            this.value2 = this.value2.slice(0, this.value2.length - 1);
        },
        switch_showKeyboard2(){
            if(!this.send){
                this.$toast({
                    message: '短信请求中...',
                    forbidClick: true
                })
            }else{
                this.showKeyboard2 = true
            }
            
        },
        next(){
            if(/^\s*$/.test(this.select_dept)){
                this.$toast({
                    message: "请选择学院",
                    forbidClick: true
                })
            }else if(/^\s*$/.test(this.name)){
                this.$toast({
                    message: "请输入姓名",
                    forbidClick: true
                })
            }

            let loading1 = this.$toast.loading({
                message: "",
                forbidClick: true,
                duration: 60000,
            })
            let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/login`
            let method = ''
            let data = {}
            switch(this.active_child){
                case 0:
                    method = `get_HUAT_teacher_EV89465Ev165r8B64`
                    data = {
                        deptID: this.select_dept,
                        teacherName: this.name.trim()
                    }
                    axios.post(`${url}/${method}`, data)
                    .then(res=>{
                        loading1.clear()
                        if(res.data.code == 0){
                            if(res.data.data.length == 0){
                                this.$toast({
                                    message: `无结果`,
                                    forbidClick: true
                                })
                            }else{
                                this.searchResult = res.data.data
                                this.active_child = 1
                            }
                        }else{
                            this.$toast({
                                message: `服务出错[${res.data.code}]`,
                                forbidClick: true
                            })

                        }
                    })
                    break;

                case 1:
                    console.log(this.selectResult)
                    method = `get_teacher_phone_687egr1143G8ER`
                    data = {
                        deptID: this.select_dept,
                        huat_id: this.selectResult
                    }
                    axios.post(`${url}/${method}`, data)
                    .then(res=>{
                        loading1.clear()
                        if(res.data.code == 0){
                            // console.log(res.data.data)
                            if(res.data.data.length == 0){//无可用手机号
                                this.$toast({
                                    message: `无可用手机号`,
                                    forbidClick: true
                                })
                            }else if(res.data.data.length == 1){//一个可用手机号
                                this.phone = res.data.data[0]
                                this.active = 1
                                this.active_child = 2
                            }else if(res.data.data.length > 1){//绑定多个手机号
                                vm.show = true
                                vm.actions = []
                                res.data.data.forEach((item, index)=>{
                                    vm.actions.push({
                                        name: item
                                    })
                                })

                            }else{
                                this.$toast({
                                    message: `服务出错`,
                                    forbidClick: true
                                })
                            }
                        }else{
                            this.$toast({
                                message: `服务出错[${res.data.code}]`,
                                forbidClick: true
                            })
                        }
                    })
                    break;
                    
            }
            
        },
    },
    mounted() {
        
        
    }
})